<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class news extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$arr = array();
		$ncrow = spClass('m_ncat')->findAll();
		foreach($ncrow as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'ncatname'=>$v['ncatname'],'odnum'=>$v['odnum']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','ncatname','format')));
		$this->ncat = $treeObj->getTree($arr);
	}
	//新闻列表
	public function lis(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($search['cid'] != ''){
				$where .= " AND cid = {$search[cid]}";
			}
			if($title = trim($search['title'])){
				$title_s = spClass('m_news')->escape('%' . $title . '%');
				$where .= " AND title LIKE {$title_s}";
				$search['title'] = $title;
			}
			$this->search = $search;
		}else{
			if($cid = $this->spArgs('cid')){
				$where .= " AND cid = {$cid}";
			}
			if($title = $this->spArgs('title')){
				$title = urldecode($title);
				$where .= " AND title LIKE '%{$title}%'";
			}
			$this->search = array('cid'=>$cid,'title'=>$title);
		}
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->news = spClass('m_news')->spLinker()->spPager($page,15)->findAll($where,'id DESC','id,cid,title,time,views,stick,odnum,visible');
		$this->pager = spClass('m_news')->spPager()->getPager();
		$this->display('news_lis.tpl.php');
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_news')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('news','lis'));
	}
	//更新浏览
	public function updvs(){
		$idrow = $this->spArgs('idrow');
		$views = $this->spArgs('views');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_news')->update(array('id'=>$idrow[$i]),array('views'=>(int)$views[$i]));
		}
		$this->success('更新浏览成功！',spUrl('news','lis'));
	}
	//添加新闻
	public function add(){
		$this->display('news_add.tpl.php');
	}
	//执行添加
	public function doadd(){
		if(false != spClass('m_news')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('news','add'));
		$rows = $this->spArgs();
		if(!$rows['visible'])	$rows['visible'] = 0;
		if(!$rows['stick'])	$rows['stick'] = 0;
		$rows['time'] = time();
		if(false != spClass('m_news')->create($rows)){
			$this->success('添加新闻成功！',spUrl('news','lis'));
		}else{
			$this->error('添加新闻失败！',spUrl('news','add'));
		}
	}
	//更新新闻
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('news','lis'));
		$this->news = spClass('m_news')->find(array('id'=>$id));
		$this->display('news_upd.tpl.php');
	}
	//执行更新
	public function doupd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('news','lis'));
		if(false != spClass('m_news')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('news','lis'));
		$rows = $this->spArgs();
		if(!$rows['visible']) $rows['visible'] = 0;
		if(!$rows['stick']) $rows['stick'] = 0;
		$thumb = spClass('m_news')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb']) && !empty($rows['thumb']) && $thumb['thumb']!=$rows['thumb']){
			$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			@unlink($thumb['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_news')->update(array('id'=>$id),$rows)){
			$this->success('更新新闻成功！',spUrl('news','lis'));
		}else{
			$this->error('更新新闻失败！',spUrl('news','lis'));
		}
	}
	//删除新闻
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('news','lis'));
		$thumb = spClass('m_news')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb'])){
			if(strpos($thumb['thumb'], 'thumb_') !== false){
				$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			}else{
				$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
			}
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_news')->delete(array('id'=>$id))){
			$this->success('删除新闻成功！',spUrl('news','lis'));
		}else{
			$this->error('删除新闻失败！',spUrl('news','lis'));
		}
	}
	//批量删除
	public function pldel(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要删除的文章编号！',spUrl('news','lis'));
		for($i=0;$i<count($idrow);$i++){
			$thumb = spClass('m_news')->find(array('id'=>$idrow[$i]),null,'thumb');
			if(!empty($thumb['thumb'])){
				if(strpos($thumb['thumb'], 'thumb_') !== false){
					$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
				}else{
					$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
				}
				if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
				if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
				spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
			}
			spClass('m_news')->delete(array('id'=>$idrow[$i]));
		}
		$this->success('批量删除新闻成功！',spUrl('news','lis'));
	}
}
